<?php 
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class PwdController extends AdminBaseController{
	public function index(){
		$this -> display();
	}

	public function save(){
		if(IS_POST || IS_AJAX){
			$pwdlist = M('Admin')->field('id,name,pwd')->where(['name' => session('user.account')])->find();
			$oldpwd = I('post.oldpwd/s','');
			if(empty($oldpwd)){
				$this -> ajaxReturn(['status' => 0,'原始密码不能为空！']);
			}
			if(_md5($oldpwd) != $pwdlist['pwd']){
				$this -> ajaxReturn(['status' => 0, '原始密码不正确！']);
			}
			$newpwd = I('post.newpwd/s','');
			if(empty($newpwd)){
				$this -> ajaxReturn(['status' => 0, '新密码不能为空！']);
			}
			if((strlen($newpwd) < 6) || (strlen($newpwd) > 15)){
				$this -> ajaxReturn(['status' => 0, 'mess' => '新密码长度不符合要求！']);
			}
			$repwd = I('post.repwd/s','');
			if(empty($repwd)){
				$this -> ajaxReturn(['status' => 0, '新密码不能为空！']);
			}
			if((strlen($repwd) < 6) || (strlen($repwd) > 15)){
				$this -> ajaxReturn(['status' => 0,'mess' => '新密码长度不符合要求！']);
			}
			if(strlen($newpwd) != strlen($repwd)){
				$this -> ajaxReturn(['status' => 0,'mess' => '两次输入的密码长度不一致']);
			}
			if(_md5($newpwd) != _md5($repwd)){
				$this -> ajaxReturn(['status' => 0, 'mess' => '两次输入的密码不一样！']);
			}

			$is_up = M('Admin')->where(['name'=>session('user.account')])->setField(['pwd'=>_md5($newpwd)]);

			if($is_up === false){
				$this -> ajaxReturn(['status' => 0, 'mess' => '修改失败']);
			}else{
				$this -> ajaxReturn(['status' => 1, 'mess' => '修改成功']);
			}
		}
	}




}
